home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / INS.CAB / regfilt.vbs < prev    next >
Encoding:
Text File  |  2003-02-21  |  11.5 KB  |  342 lines

  1. Option Explicit
  2.  
  3. '
  4. ' these GUIDs are all defined in nntpfilt.idl
  5. '
  6. ' the OnPostEarly event GUID
  7. Const GUIDComCatOnPostEarly = "{C028FD86-F943-11d0-85BD-00C04FB960EA}"
  8. ' the OnPost event GUID
  9. Const GUIDComCatOnPost = "{C028FD83-F943-11d0-85BD-00C04FB960EA}"
  10. ' the OnPostFinal event GUID
  11. Const GUIDComCatOnPostFinal = "{C028FD85-F943-11d0-85BD-00C04FB960EA}"
  12. ' the NNTP source type
  13. Const GUIDSourceType = "{C028FD82-F943-11d0-85BD-00C04FB960EA}"
  14.  
  15. ' the NNTP service display name.  This is used to key which service to
  16. ' edit
  17. Const szService = "nntpsvc"
  18.  
  19. ' the event manager object.  This is used to communicate with the 
  20. ' event binding database.
  21. Dim EventManager
  22. Set EventManager = WScript.CreateObject("Event.Manager")
  23.  
  24. '
  25. ' register a new sink with event manager
  26. '
  27. ' iInstance - the instance to work against
  28. ' szEvent - OnPostEarly, OnPost or OnPostFinal
  29. ' szDisplayName - the display name for this new sink
  30. ' szProgID - the progid to call for this event
  31. ' szRule - the rule to set for this event
  32. '
  33. public sub RegisterSink(iInstance, szEvent, szDisplayName, szProgID, szRule)
  34.     Dim SourceType
  35.     Dim szSourceDisplayName
  36.     Dim Source
  37.     Dim Binding
  38.     Dim GUIDComCat
  39.  
  40.     ' figure out which event they are trying to register with and set
  41.     ' the comcat for this event in GUIDComCat
  42.     select case LCase(szEvent)
  43.         case "onpostearly"
  44.             GUIDComCat = GUIDComCatOnPostEarly
  45.         case "onpost"
  46.             GUIDComCat = GUIDComCatOnPost
  47.         case "onpostfinal"
  48.             GUIDComCat = GUIDComCatOnPostFinal
  49.         case else
  50.             WScript.echo "invalid event: " + szEvent
  51.             exit sub
  52.     end select
  53.  
  54.     ' enumerate through each of the registered instances for the NNTP source
  55.     ' type and look for the display name that matches the instance display
  56.     ' name
  57.     set SourceType = EventManager.SourceTypes(GUIDSourceType)
  58.     szSourceDisplayName = szService + " " + iInstance
  59.     for each Source in SourceType.Sources
  60.         if Source.DisplayName = szSourceDisplayName then
  61.             ' we've found the desired instance.  now add a new binding
  62.             ' with the right event GUID.  by not specifying a GUID to the
  63.             ' Add method we get server events to create a new ID for this
  64.             ' event
  65.             set Binding = Source.GetBindingManager.Bindings(GUIDComCat).Add("")
  66.             ' set the binding properties
  67.             Binding.DisplayName = szDisplayName
  68.             Binding.SinkClass = szProgID
  69.             ' register a rule with the binding
  70.             Binding.SourceProperties.Add "Rule", szRule
  71.             ' save the binding
  72.             Binding.Save
  73.             WScript.Echo "registered " + szDisplayName
  74.             exit sub
  75.         end if
  76.     next
  77. end sub
  78.  
  79. '
  80. ' unregister a previously registered sink
  81. '
  82. ' iInstance - the instance to work against
  83. ' szEvent - OnPostEarly, OnPost or OnPostFinal
  84. ' szDisplayName - the display name of the event to remove
  85. '
  86. public sub UnregisterSink(iInstance, szEvent, szDisplayName)
  87.     Dim SourceType
  88.     Dim GUIDComCat
  89.     Dim szSourceDisplayName
  90.     Dim Source
  91.     Dim Bindings
  92.     Dim Binding
  93.  
  94.     select case LCase(szEvent)
  95.         case "onpostearly"
  96.             GUIDComCat = GUIDComCatOnPostEarly
  97.         case "onpost"
  98.             GUIDComCat = GUIDComCatOnPost
  99.         case "onpostfinal"
  100.             GUIDComCat = GUIDComCatOnPostFinal
  101.         case else
  102.             WScript.echo "invalid event: " + szEvent
  103.             exit sub
  104.     end select
  105.  
  106.     ' find the source for this instance
  107.     set SourceType = EventManager.SourceTypes(GUIDSourceType)
  108.     szSourceDisplayName = szService + " " + iInstance
  109.     for each Source in SourceType.Sources
  110.         if Source.DisplayName = szSourceDisplayName then
  111.             ' find the binding by display name.  to do this we enumerate
  112.             ' all of the bindings and try to match on the display name
  113.             set Bindings = Source.GetBindingManager.Bindings(GUIDComCat)
  114.             for each Binding in Bindings
  115.                 if Binding.DisplayName = szDisplayName then
  116.                     ' we've found the binding, now remove it
  117.                     Bindings.Remove(Binding.ID)
  118.                     WScript.Echo "removed " + szDisplayName + " " + Binding.ID
  119.                 end if
  120.             next
  121.         end if
  122.     next
  123. end sub
  124.  
  125. '
  126. ' add or remove a property from the source or sink propertybag for an event
  127. '
  128. ' iInstance - the NNTP instance to edit
  129. ' szEvent - the event type (OnPostEarly, OnPost or OnPostFinal)
  130. ' szDisplayName - the display name of the event
  131. ' szPropertyBag - the property bag to edit ("source" or "sink")
  132. ' szOperation - "add" or "remove"
  133. ' szPropertyName - the name to edit in the property bag
  134. ' szPropertyValue - the value to assign to the name (ignored for remove)
  135. '
  136. public sub EditProperty(iInstance, szEvent, szDisplayName, szPropertyBag, szOperation, szPropertyName, szPropertyValue)
  137.     Dim SourceType
  138.     Dim GUIDComCat
  139.     Dim szSourceDisplayName
  140.     Dim Source
  141.     Dim Bindings
  142.     Dim Binding
  143.     Dim PropertyBag
  144.  
  145.     select case LCase(szEvent)
  146.         case "onpostearly"
  147.             GUIDComCat = GUIDComCatOnPostEarly
  148.         case "onpost"
  149.             GUIDComCat = GUIDComCatOnPost
  150.         case "onpostfinal"
  151.             GUIDComCat = GUIDComCatOnPostFinal
  152.         case else
  153.             WScript.echo "invalid event: " + szEvent
  154.             exit sub
  155.     end select
  156.  
  157.     ' find the source for this instance
  158.     set SourceType = EventManager.SourceTypes(GUIDSourceType)
  159.     szSourceDisplayName = szService + " " + iInstance
  160.     for each Source in SourceType.Sources
  161.         if Source.DisplayName = szSourceDisplayName then
  162.             set Bindings = Source.GetBindingManager.Bindings(GUIDComCat)
  163.             ' find the binding by display name.  to do this we enumerate
  164.             ' all of the bindings and try to match on the display name
  165.             for each Binding in Bindings
  166.                 if Binding.DisplayName = szDisplayName then
  167.                     ' figure out which set of properties we want to modify
  168.                     ' based on the szPropertyBag parameter
  169.                     select case LCase(szPropertyBag)
  170.                         case "source"
  171.                             set PropertyBag = Binding.SourceProperties
  172.                         case "sink"
  173.                             set PropertyBag = Binding.SinkProperties
  174.                         case else
  175.                             WScript.echo "invalid propertybag: " + szPropertyBag
  176.                             exit sub
  177.                     end select
  178.                     ' figure out what operation we want to perform
  179.                     select case LCase(szOperation)
  180.                         case "remove"
  181.                             ' they want to remove szPropertyName from the
  182.                             ' property bag
  183.                             PropertyBag.Remove szPropertyName
  184.                             WScript.echo "removed property " + szPropertyName
  185.                         case "add"
  186.                             ' add szPropertyName to the property bag and 
  187.                             ' set its value to szValue.  if this value
  188.                             ' already exists then this will change  the value
  189.                             ' it to szValue.
  190.                             PropertyBag.Add szPropertyName, szPropertyValue
  191.                             WScript.echo "set property " + szPropertyName + " to " + szPropertyValue
  192.                         case else
  193.                             WScript.echo "invalid operation: " + szOperation
  194.                             exit sub
  195.                     end select
  196.                     ' save the binding
  197.                     Binding.Save
  198.                 end if
  199.             next
  200.         end if
  201.     next
  202. end sub
  203.  
  204. '
  205. ' this helper function takes an IEventSource object and a event category
  206. ' and dumps all of the bindings for this category under the source
  207. '
  208. ' Source - the IEventSource object to display the bindings for
  209. ' GUIDComCat - the event category to display the bindings for
  210. '
  211. public sub DisplaySinksHelper(Source, GUIDComCat)
  212.     Dim Binding
  213.     Dim pProperty
  214.     ' walk each of the registered bindings for this component category
  215.     for each Binding in Source.GetBindingManager.Bindings(GUIDComCat)
  216.         ' display the binding properties
  217.         WScript.echo "    Binding " + Binding.ID + " {"
  218.         WScript.echo "      DisplayName = " + Binding.DisplayName
  219.         WScript.echo "      SinkClass = " + Binding.SinkClass
  220.  
  221.         ' walk each of the source properties and display them
  222.         WScript.echo "      SourceProperties {"
  223.         for each pProperty in Binding.SourceProperties
  224.             WScript.echo "        " + pProperty + " = " + Binding.SourceProperties.Item(pProperty)
  225.         next
  226.         WScript.echo "      }"
  227.  
  228.         ' walk each of the sink properties and display them
  229.         WScript.echo "      SinkProperties {"
  230.         for each pProperty in Binding.SinkProperties
  231.             WScript.echo "        " + pProperty + " = " + Binding.SinkProperties.Item(pProperty)
  232.         next
  233.         WScript.echo "      }"
  234.         WScript.echo "    }"
  235.     next
  236. end sub
  237.  
  238. '
  239. ' dumps all of the information in the binding database related to NNTP
  240. '
  241. public sub DisplaySinks
  242.     Dim SourceType
  243.     Dim Source
  244.  
  245.     ' look for each of the sources registered for the NNTP source type
  246.     set SourceType = EventManager.SourceTypes(GUIDSourceType)
  247.     for each Source in SourceType.Sources
  248.         ' display the source properties
  249.         WScript.echo "Source " + Source.ID + " {"
  250.         WScript.echo "  DisplayName = " + Source.DisplayName
  251.         ' display all of the sinks registered for the OnPost event
  252.         WScript.echo "  OnPostEarly Sinks {"
  253.         call DisplaySinksHelper(Source, GUIDComCatOnPostEarly)
  254.         WScript.echo "  }"
  255.         WScript.echo "  OnPost Sinks {"
  256.         call DisplaySinksHelper(Source, GUIDComCatOnPost)
  257.         WScript.echo "  }"
  258.         ' display all of the sinks registered for the OnPostFinal event
  259.         WScript.echo "  OnPostFinal Sinks {"
  260.         call DisplaySinksHelper(Source, GUIDComCatOnPostFinal)
  261.         WScript.echo "  }"
  262.         WScript.echo "}"
  263.     next
  264. end sub
  265.  
  266. ' display usage information for this script
  267. '
  268. public sub DisplayUsage
  269.     WScript.echo "usage: cscript regfilt.vbs <command> <arguments>"
  270.     WScript.echo "  commands:"
  271.     WScript.echo "    /add <Instance> <Event> <DisplayName> <SinkClass> <Rule>"
  272.     WScript.echo "    /remove <Instance> <Event> <DisplayName>"
  273.     WScript.echo "    /setprop <Instance> <Event> <DisplayName> <PropertyBag> <PropertyName> "
  274.     WScript.echo "             <PropertyValue>"
  275.     WScript.echo "    /delprop <Instance> <Event> <DisplayName> <PropertyBag> <PropertyName>"
  276.     WScript.echo "    /enum"
  277.     WScript.echo "  arguments:"
  278.     WScript.echo "    <Instance> is the NNTP instance to work against"
  279.     WScript.echo "    <Event> can be OnPostEarly, OnPost or OnPostFinal"
  280.     WScript.echo "    <DisplayName> is the display name of the event to edit"
  281.     WScript.echo "    <SinkClass> is the sink class for the event"
  282.     WScript.echo "    <Rule> is the rule to use for the event"
  283.     WScript.echo "    <PropertyBag> can be Source or Sink"
  284.     WScript.echo "    <PropertyName> is the name of the property to edit"
  285.     WScript.echo "    <PropertyValue> is the value to assign to the property"
  286. end sub
  287.  
  288.  
  289. Dim iInstance
  290. Dim szEvent
  291. Dim szDisplayName
  292. Dim szSinkClass
  293. Dim szRule
  294. Dim szPropertyBag
  295. Dim szPropertyName
  296. Dim szPropertyValue
  297.  
  298. '
  299. ' this is the main body of our script.  it reads the command line parameters
  300. ' specified and then calls the appropriate function to perform the operation
  301. '
  302. if WScript.Arguments.Count = 0 then
  303.     call DisplayUsage
  304. else 
  305.     Select Case LCase(WScript.Arguments(0))
  306.         Case "/add"
  307.             iInstance = WScript.Arguments(1)
  308.             szEvent = WScript.Arguments(2)
  309.             szDisplayName = WScript.Arguments(3)
  310.             szSinkClass = WScript.Arguments(4)
  311.             szRule = WScript.Arguments(5)
  312.             call RegisterSink(iInstance, szEvent, szDisplayName, szSinkClass, szRule)
  313.         Case "/remove"
  314.             iInstance = WScript.Arguments(1)
  315.             szEvent = WScript.Arguments(2)
  316.             szDisplayName = WScript.Arguments(3)
  317.             call UnregisterSink(iInstance, szEvent, szDisplayName)        
  318.         Case "/setprop"
  319.             iInstance = WScript.Arguments(1)
  320.             szEvent = WScript.Arguments(2)
  321.             szDisplayName = WScript.Arguments(3)
  322.             szPropertyBag = WScript.Arguments(4)
  323.             szPropertyName = WScript.Arguments(5)
  324.             szPropertyValue = WScript.Arguments(6)
  325.             call EditProperty(iInstance, szEvent, szDisplayName, szPropertyBag, "add", szPropertyName, szPropertyValue)
  326.         Case "/delprop"
  327.             iInstance = WScript.Arguments(1)
  328.             szEvent = WScript.Arguments(2)
  329.             szDisplayName = WScript.Arguments(3)
  330.             szPropertyBag = WScript.Arguments(4)
  331.             szPropertyName = WScript.Arguments(5)
  332.             call EditProperty(iInstance, szEvent, szDisplayName, szPropertyBag, "remove", szPropertyName, "")        
  333.         Case "/dump"
  334.             call DisplaySinks
  335.         Case "/enum"
  336.             call DisplaySinks
  337.         Case Else
  338.             call DisplayUsage
  339.     End Select
  340. end if
  341.